package com.onlineexam.controller;


import com.onlineexam.bean.User;
import com.onlineexam.mapper.UserMapper;
import com.onlineexam.util.JsonResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

//允许其他项目访问我的数据
@CrossOrigin("*")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserMapper UserMapper;

    @RequestMapping("/login")
    public JsonResult login(User user) { //参数
        System.out.println(user);
        User u = UserMapper.login(user);
        if (u == null) {
            // 登陆失败
            JsonResult result = new JsonResult(false, 427, null, "账号或者密码错误");
            return result;
        } else {
            // 登陆成功 将用户登录信息送至前端 userid
            JsonResult result = new JsonResult(true, 200, u, "");
            // 保存当前登录信息
            return result;
        }
    }

    @RequestMapping("/reg")
    public JsonResult reg(User user) {
        UserMapper.reg(user);
        JsonResult result = new JsonResult(true, 200, "success", "");
        return result;
        }

    @RequestMapping("/list") //用户列表
    public JsonResult UserList() {
        List<User> list = UserMapper.list();
        JsonResult result = new JsonResult(true, 200, list, "");
        return result;
        }




    }




